﻿using Coldairarrow.Business;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Threading.Channels;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using static CSRedis.CSRedisClient;

namespace Coldairarrow.Api.OrderHelper
{
    public static class OrderHelperService
    {
        private static void Log()
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A_log.txt");
            using (StreamWriter sw = new StreamWriter(filePath, true))
            {
                sw.WriteLine(DateTime.Now.ToLongTimeString());
            }
        }

        public static IWebHostBuilder UseRedisTwo(this IWebHostBuilder hostBuilder)
        {
            var csredis = new CSRedis.CSRedisClient("127.0.0.1:6379");
            RedisHelper.Initialization(csredis);
            //var channel = Channel.CreateUnbounded<string>();
            //var reader = channel.Reader;

            //IOrder_MasterBusiness _MasterBusiness;
              CannclOrder canncl = new CannclOrder();
            hostBuilder.ConfigureServices( (buidlerContext, services) =>
            {
                IServiceProvider service = services.BuildServiceProvider();

              var calcService = service.GetService<IOrder_MasterBusiness>();
             
               
                RedisHelper.Subscribe(("__keyevent@0__:expired", (msg) =>
                {
                    string Id = msg.Body;
                    if (Id.Length > 5)
                    {
                        calcService.CancelOrder(Id);


                    }
                }
                ));

               
            });
            //while ( reader.WaitToReadAsync().Result)
            //{
            //    if (reader.TryRead(out var number))
            //    {

            //        Console.WriteLine(number);
            //        canncl.
            //    }
            //}
            return hostBuilder;
        }

        private static void CancaelOrder()
        {

           
        }
    }
}